package mapReduce.demo06_sort.demo02_reverseIndex;

import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;

public class ReverseIndexCountNewCombiner extends Reducer<Text,Text,Text,Text> {

    Text outKey = new Text();
    Text outValue = new Text();

    @Override
    protected void reduce(Text key, Iterable<Text> values, Reducer<Text, Text, Text, Text>.Context context) throws IOException, InterruptedException {
        /**
         * 接收到数据的格式
         *      商品名#平台名     1
         */

        int sum = 0;
        for (Text value : values) {
            sum += Integer.parseInt(value.toString());
        }

        String productPlatform = key.toString();
        String[] fields = productPlatform.split("#");

        outKey.set(fields[0]);
        outValue.set(fields[1]+":"+sum);

        /**
         * 输出数据的格式
         *      商品名     平台名:数量
         */
        context.write(outKey,outValue);

    }
}
